package com.ruoyi.gams.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.gams.vo.GamsDeviceBussinessRelationAddParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gams.domain.GamsDeviceBussinessRelation;
import com.ruoyi.gams.service.IGamsDeviceBussinessRelationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备业务关系Controller
 *
 * @author ruoyi
 */
@Slf4j
@Api(tags = "广告系统后台：设备业务关系")
@RestController
@RequestMapping("/gams/device_relation")
public class GamsDeviceBussinessRelationController extends BaseController
{
    @Autowired
    private IGamsDeviceBussinessRelationService gamsDeviceBussinessRelationService;

    /**
     * 查询设备业务关系列表
     */
    @ApiOperation("查询设备业务关系列表")
    @PreAuthorize("@ss.hasPermi('gams:device_relation:list')")
    @GetMapping("/list")
    public TableDataInfo list(GamsDeviceBussinessRelation gamsDeviceBussinessRelation)
    {
        startPage();
        List<GamsDeviceBussinessRelation> list = gamsDeviceBussinessRelationService.selectGamsDeviceBussinessRelationList(gamsDeviceBussinessRelation);
        return getDataTable(list);
    }

    /**
     * 导出设备业务关系列表
     */
    @ApiOperation("导出设备业务关系列表")
    @PreAuthorize("@ss.hasPermi('gams:device_relation:export')")
    @Log(title = "导出设备业务关系列表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(GamsDeviceBussinessRelation gamsDeviceBussinessRelation)
    {
        List<GamsDeviceBussinessRelation> list = gamsDeviceBussinessRelationService.selectGamsDeviceBussinessRelationList(gamsDeviceBussinessRelation);
        ExcelUtil<GamsDeviceBussinessRelation> util = new ExcelUtil<>(GamsDeviceBussinessRelation.class);
        return util.exportExcel(list, "设备业务关系数据");
    }

    /**
     * 获取设备业务关系详细信息
     */
    @ApiOperation("获取设备业务关系详细信息")
    @PreAuthorize("@ss.hasPermi('gams:device_relation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(gamsDeviceBussinessRelationService.selectGamsDeviceBussinessRelationById(id));
    }

    /**
     * 新增设备业务关系
     */
    @ApiOperation("新增设备业务关系")
    @PreAuthorize("@ss.hasPermi('gams:device_relation:add')")
    @Log(title = "新增设备业务关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GamsDeviceBussinessRelation gamsDeviceBussinessRelation)
    {
        return toAjax(gamsDeviceBussinessRelationService.insertGamsDeviceBussinessRelation(gamsDeviceBussinessRelation));
    }

    /**
     * 修改设备业务关系
     */
    @ApiOperation("修改设备业务关系")
    @PreAuthorize("@ss.hasPermi('gams:device_relation:edit')")
    @Log(title = "修改设备业务关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GamsDeviceBussinessRelation gamsDeviceBussinessRelation)
    {
        return toAjax(gamsDeviceBussinessRelationService.updateGamsDeviceBussinessRelation(gamsDeviceBussinessRelation));
    }

    /**
     * 删除设备业务关系
     */
    @ApiOperation("删除设备业务关系")
    @PreAuthorize("@ss.hasPermi('gams:device_relation:remove')")
    @Log(title = "删除设备业务关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(gamsDeviceBussinessRelationService.deleteGamsDeviceBussinessRelationByIds(ids));
    }

    /**
     * 批量新增设备业务关系
     */
    @ApiOperation("批量新增设备业务关系")
    @PostMapping("/insertBatch")
    public AjaxResult insertBatch(@RequestBody GamsDeviceBussinessRelationAddParam param)
    {
        int i = gamsDeviceBussinessRelationService.deleteByDeviceId(param.getDeviceId());
        log.info("删除原关系记录数：" + i);
        return toAjax(gamsDeviceBussinessRelationService.insertBatch(param));
    }

    /**
     * 查询设备业务关系列表
     */
    @ApiOperation("查询设备业务关系列表")
    @PreAuthorize("@ss.hasPermi('gams:device_relation:list')")
    @GetMapping("/findBussinessIdByDeviceId")
    public TableDataInfo findBussinessIdByDeviceId(Integer deviceId, Integer type)
    {
        List<Integer> bussinessIdByDeviceId = gamsDeviceBussinessRelationService.findBussinessIdByDeviceId(deviceId, type);
        return getDataTable(bussinessIdByDeviceId);
    }
}
